package org.dromara.iot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.iot.domain.IotFaultManage;
import org.dromara.iot.domain.bo.IotFaultManageBo;
import org.dromara.iot.domain.vo.IotFaultManageVo;
import org.dromara.iot.mapper.IotFaultManageMapper;
import org.dromara.iot.service.IIotFaultManageService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 故障管理Service业务层处理
 *
 * @author lsl
 * @date 2024-06-15
 */
@RequiredArgsConstructor
@Service
public class IotFaultManageServiceImpl implements IIotFaultManageService {

    private final IotFaultManageMapper baseMapper;

    /**
     * 查询故障管理
     */
    @Override
    public IotFaultManageVo queryById(String faultId) {
        return baseMapper.selectVoById(faultId);
    }

    /**
     * 查询故障管理列表
     */
    @Override
    public TableDataInfo<IotFaultManageVo> queryPageList(IotFaultManageBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IotFaultManage> lqw = buildQueryWrapper(bo);
        Page<IotFaultManageVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询故障管理列表
     */
    @Override
    public List<IotFaultManageVo> queryList(IotFaultManageBo bo) {
        LambdaQueryWrapper<IotFaultManage> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IotFaultManage> buildQueryWrapper(IotFaultManageBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IotFaultManage> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getBrandId() != null, IotFaultManage::getBrandId, bo.getBrandId());
        lqw.like(StringUtils.isNotBlank(bo.getBrandName()), IotFaultManage::getBrandName, bo.getBrandName());
        lqw.eq(bo.getModelId() != null, IotFaultManage::getModelId, bo.getModelId());
        lqw.eq(StringUtils.isNotBlank(bo.getModelIds()), IotFaultManage::getModelIds, bo.getModelIds());
        lqw.like(StringUtils.isNotBlank(bo.getModelName()), IotFaultManage::getModelName, bo.getModelName());
        lqw.eq(StringUtils.isNotBlank(bo.getFaultCode()), IotFaultManage::getFaultCode, bo.getFaultCode());
        lqw.eq(StringUtils.isNotBlank(bo.getFaultDescribe()), IotFaultManage::getFaultDescribe, bo.getFaultDescribe());
        lqw.eq(StringUtils.isNotBlank(bo.getPossibleCauses()), IotFaultManage::getPossibleCauses, bo.getPossibleCauses());
        lqw.eq(StringUtils.isNotBlank(bo.getDefectiveElement()), IotFaultManage::getDefectiveElement, bo.getDefectiveElement());
        lqw.eq(StringUtils.isNotBlank(bo.getElevatorStatus()), IotFaultManage::getElevatorStatus, bo.getElevatorStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getRecovery()), IotFaultManage::getRecovery, bo.getRecovery());
        lqw.eq(StringUtils.isNotBlank(bo.getExperiment()), IotFaultManage::getExperiment, bo.getExperiment());
        return lqw;
    }

    /**
     * 新增故障管理
     */
    @Override
    public Boolean insertByBo(IotFaultManageBo bo) {
        IotFaultManage add = MapstructUtils.convert(bo, IotFaultManage.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setFaultId(add.getFaultId());
        }
        return flag;
    }

    /**
     * 修改故障管理
     */
    @Override
    public Boolean updateByBo(IotFaultManageBo bo) {
        IotFaultManage update = MapstructUtils.convert(bo, IotFaultManage.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IotFaultManage entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除故障管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
